import React from 'react';
import {PubSubManager} from "../QtUtils";
import {
  COMPONENT_TYPE_MENU,
  COMPONENT_TYPE_VIEW,
  DIRECTION_HIDE_BOTTOM,
  DIRECTION_HIDE_LEFT,
  DIRECTION_HIDE_RIGHT,
  DIRECTION_HIDE_TOP,
  FrameManager
} from "./FrameManager";

class BaseReact extends React.Component {
    constructor(props) {
        super(props);

        this.unique_name = 'base'
        this.type = COMPONENT_TYPE_VIEW
        this.state = {
          isSelfShow:true,
          isMounted:false,
          isLeftHide:false,     //左侧区域是否隐藏
          isRightHide:false,     //右侧区域是否隐藏
          isTopHide:false,     //上侧区域是否隐藏
          isBottomHide:false,     //下侧区域是否隐藏
          componentzIndex:1,        //zIndex
        }
    }
    componentDidMount() {
        if(this.type == COMPONENT_TYPE_MENU) {
          //菜单类型的则做唯一校验
          if(this.unique_name == 'base' || this.unique_name == undefined || this.unique_name == null) {
            let msg = 'unique_name无效--' + this.unique_name;
            alert(msg);
            throw(msg);
          }
          FrameManager.getInstance().addMenu(this.unique_name);
        }
        FrameManager.getInstance().addComponentName(this.unique_name);
        this.setState({
          isMounted:true
        })

        this.onMounted();
    }
    componentWillMount() {
        this.onCreated();
        //接收消息
        PubSubManager.addEvent(this,'PubSubMessage',(msg,data)=>{
          let strCode = data['strCode'];
          let excludeComponent = data['excludeComponent'];
          let realData = data['data'];
          if(excludeComponent.length == 0 || !excludeComponent.includes(this.unique_name)) {
            this.onPubSubMessage(strCode,realData);
          }
        })

        //监听切换Component的消息
        PubSubManager.addEvent(this,'switchComponent',(msg,data)=>{
          let curName = data['curName'];
          let lastName = data['lastName'];
          this.onSwitchComponent(curName,lastName);
        })

        //页面显隐切换
        PubSubManager.addEvent(this,'preViewShow',(msg,data)=>{
          let strName = data[0];
          let isShow = data[1];
          let zIndex = data[2];
          if(strName == this.unique_name) {
              let viewParam = FrameManager.getInstance().GetViewParam(strName);
              let isChange = (isShow != viewParam.isPreShow);
              if(isShow && isChange) {
                  this.onPreEnter();
              }else if(!isShow && isChange){
                  this.onPreExit();
              }
          }
        })

        //执行visible事件
        PubSubManager.addEvent(this,'setViewShow',(msg,data)=>{
            let strName = data[0];
            let isShow = data[1];
            let zIndex = data[2];
            if(strName == this.unique_name) {
                this.setState({
                    isSelfShow:isShow,
                    componentzIndex:zIndex,
                })
            }
        })

        //执行visible事件
        PubSubManager.addEvent(this,'postViewShow',(msg,data)=>{
            let strName = data[0];
            let isShow = data[1];
            let zIndex = data[2];
            if(strName == this.unique_name) {
                let viewParam = FrameManager.getInstance().GetViewParam(strName);
                let isChange = (isShow != viewParam.isPreShow);
                if(isShow && isChange) {
                    this.onEnter();
                }else if(!isShow && isChange){
                    this.onExit();
                }
            }
            this.onViewVisible(strName,isShow,zIndex);
        })

        //执行visible事件
        PubSubManager.addEvent(this,'setViewComponentDirectHide',(msg,data)=>{
          this.setState({
            isLeftHide:false,
            isRightHide:false,
            isTopHide:false,
            isBottomHide:false
          })

          let nDirectCode = data[0];
          if(nDirectCode&DIRECTION_HIDE_LEFT) {
            this.setState({
              isLeftHide:true
            })
          }
          if(nDirectCode&DIRECTION_HIDE_RIGHT) {
            this.setState({
              isRightHide:true
            })
          }
          if(nDirectCode&DIRECTION_HIDE_TOP) {
            this.setState({
              isTopHide:true
            })
          }
          if(nDirectCode&DIRECTION_HIDE_BOTTOM) {
            this.setState({
              isBottomHide:true
            })
          }
          this.onDirectHideState(nDirectCode);
        })

        //restartEnter
        PubSubManager.addEvent(this,'restartEnter',(msg,data)=>{
          if(data.included(this.unique_name)) {
            this.onEnter();
          }
        })
        //key_frame_message
        PubSubManager.addEvent(this,'key_frame_message',(msg,data)=>{
          let frameID = data['frameID'];
          let strCode = data['code'];
          let realData = data['data'];
          this.onFrameMessage(frameID,strCode,realData);
        })
    }
    componentWillUnmount() {
        this.onExit();
        this.onDestroyed();
        PubSubManager.removeEvent(this);
        //
    }

    onMounted() {}
    onCreated() {}
    onDestroyed() {}
    /**
     * PubSub接收消息
     */
    onPubSubMessage(strCode,data) {}
    onViewVisible(strName,isShow,zIndex) {}   //页面显隐切换
    onPreEnter() {}    //还未进入（未显示前）
    onEnter() {}
    onPreExit() {}     //还未进入（未隐藏前）
    onExit() {}
    onSwitchComponent(curName,lastName) {}
    onDirectHideState(nDirectCode) {}    //页面上下左右区域的隐藏状态变化
    onFrameMessage(strFrameID,strCode,data) {}  //iframe的消息
}
export default BaseReact
